#!/usr/bin/env python
# -*- encoding: utf-8 -*-
'''
@File    :   duel
@Time    :   2023/03/30 14:21:27
@author  :   Eval
@Version :   1.0
@Describe:   战斗事件
'''
import pygame,time
from lib.scripts.Enemys import *
from lib.scripts.Hero import *

class Duel:
    def __init__(self,parent) -> None:
        from lib.run import Run
        self.parent:Run = parent
        # 战斗面板
        self.packBG = pygame.Surface((450,200))
        self.packBG.set_alpha(170)
        self.__isActive = False # 面板是否激活, 禁止从外部直接修改
        self.Player_1:Hero = self.parent.hero
        self.Player_2:Enemys = None
        # 决定战斗的出手速度
        self.p1Speed = 100
        self.p2Speed = 100
        self.resert = 100
        # 出手图标
        self.p1Icon = pygame.image.load("lib/images/editIco.png").convert_alpha()
        self.p1Icon = pygame.transform.scale(self.p1Icon,(32,32))
        self.p2Icon = pygame.image.load("lib/images/editIco_1.png").convert_alpha()
        self.p2Icon = pygame.transform.scale(self.p2Icon,(32,32))

        # 战斗是否结束
        self.duelOver = True
        # 战斗计时器
        self.timer = 0
        # 战斗开始时间
        self.startTimer = 0
        # 当前回合耗时
        self.nowTimer = 0
        # 得到每秒的时间差,用于计算技能冷却
        self.DValueTime = 0
        # 是否开启播放战斗动画过程
        self.DuelAnim = True

    
    def isActive(self):
        """返回面板是否激活状态"""
        return self.__isActive
    
    def render(self):
        """渲染面板"""
        if not self.__isActive:
            return
        
        if not self.DuelAnim:
            # 是否直接战斗,不显示战斗动画过程
            self.__hideUIDuel()
            return
        
        self.parent.win.blit(self.packBG,(200,100))
        # 左侧渲染主角
        self.Player_1.imageUI.update(self.parent,205,110)
        status1 = f"hp:{self.Player_1.hp}\n\n攻击:{self.Player_1.atk}\n\n防御:{self.Player_1.defe}\n\n速度:{self.Player_1.speed}"
        self.parent.GameFont.render_multilineText(240,110,status1,200,fontSize=12)
        # 分割线
        pygame.draw.aaline(self.parent.win,(200,200,200),(420,100),(420,300))
        # 右侧渲染怪物
        self.Player_2.imageUI.update(self.parent,430,110)
        status2 = f"{self.Player_2.name}\n\nhp:{self.Player_2.hp}\n\n攻击:{self.Player_2.atk}\n\n防御:{self.Player_2.defe}\n\n速度:{self.Player_2.speed}\n\n{self.Player_2.describe}"
        self.parent.GameFont.render_multilineText(470,110,status2,200,fontSize=12)

        # 绘制战斗条
        pygame.draw.rect(self.parent.win,(200,200,0),(310,90,200,5))
        self.parent.win.blit(self.p2Icon,(290+(100 - self.p2Speed)*2,50))
        self.parent.win.blit(self.p1Icon,(290+(100 - self.p1Speed)*2,50))
        # 战斗结束,后续的操作不执行
        if not self.duelOver:
            if(self.Player_1.hp <= 0):
                self.parent.GameFont.render_multilineText(350,65,"战斗结束,怪物胜利",200,fontSize=16)

            if(self.Player_2.hp <= 0):
                self.parent.GameFont.render_multilineText(350,65,"战斗结束,勇士胜利",200,fontSize=16)
            return
        # 计算时间差
        self.DValueTime = time.time() - self.nowTimer
        if self.DValueTime >= 1:
            self.timer += 1
            self.nowTimer = time.time()
            print(f"{self.timer}:秒")
        if self.parent.GInit.delay % 2:
            return
        # 如果正在播放攻击特效,那么先暂停出手
        if self.parent.GameEff.nowPlayEff:
            return
        self.p1Speed -= self.Player_1.speed
        self.p2Speed -= self.Player_2.speed

        if self.p1Speed <= 0:
            self.p1Speed = self.resert
            self.Player_2.hp -= max((self.Player_1.atk*10 - self.Player_2.defe),1)
            self.parent.GameEff.randPlayEff(430,110,200)
            # self.parent.GameEff.playEffect("tx1",430,110,200)

        elif self.p2Speed <= 0:
            self.p2Speed = self.resert
            self.Player_1.hp -= max(self.Player_2.atk - self.Player_1.defe,1)
            self.parent.GameEff.randPlayEff(205,110,200)

        if(self.Player_1.hp <= 0):
            # self.__isActive = False
            self.duelOver = False
            self.Player_1.hp = 0
            # print("主角死亡,游戏结束")
            self.parent.GameFont.render_multilineText(420,80,"战斗结束,怪物胜利",200,fontSize=12)

        if(self.Player_2.hp <= 0):
            # self.__isActive = False
            self.Player_2.hp = 0
            self.duelOver = False
            self.parent.MonsterBook.clearMonsterBook()
            # print("战斗结束",self.Player_2)
            self.Player_2.active = False
            self.Player_1.clashOver(self.Player_2)
            self.parent.mapLogic.updataFPoint(self.Player_2.localPos[1],self.Player_2.localPos[0],0)
            self.parent.GameFont.render_multilineText(420,80,"战斗结束,勇士胜利",200,fontSize=12)
            # 战斗计时器
            self.timer = 0


    def __hideUIDuel(self):
        """不显示ui战斗"""
        if not self.duelOver:
            return
        self.Player_2.hp -= max((self.Player_1.atk*10 - self.Player_2.defe),1) * 10  # 不显示ui就增加点伤害,加速解决战斗
        self.Player_1.hp -= max(self.Player_2.atk - self.Player_1.defe,1) * 10
        if(self.Player_1.hp <= 0):
            self.duelOver = False
            self.Player_1.hp = 0
            ShowDiaLog.showTips("主角死亡,游戏结束")

        if(self.Player_2.hp <= 0):
            self.Player_2.hp = 0
            self.duelOver = False
            self.parent.MonsterBook.clearMonsterBook()
            # print("战斗结束",self.Player_2)
            self.Player_2.active = False
            self.Player_1.clashOver(self.Player_2)
            self.parent.mapLogic.updataFPoint(self.Player_2.localPos[1],self.Player_2.localPos[0],0)

    def startPvP(self,p1,p2):
        """触发战斗"""
        self.__isActive = True
        self.duelOver = True
        self.Player_1 = p1
        self.Player_2 = p2
        # 重置出手间隔
        self.p1Speed = self.resert
        self.p2Speed = self.resert
        self.startTimer = time.time()
        self.nowTimer = self.startTimer

    def closeDuelUI(self):
        self.__isActive = False

    def changeState(self):
        """切换是否显示战斗过程"""
        self.DuelAnim = not self.DuelAnim

    def isDuelAnim(self):
        return "开启" if self.DuelAnim else "关闭"